﻿using System.Linq;
using WeBlog.Interfaces;
using WeBlog.Models;

namespace WeBlog.Repositories
{        
    public class TagRepository : RepositoryBase<Tag>
    {        
        public TagRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {            
        }        

        public Tag FindByName( string name )
        {
            return FindAll().SingleOrDefault(x => x.Name == name);
        }

        public override void Remove(Tag entity)
        {
            entity.Posts.Clear();
            base.Remove(entity);
        }

        public IOrderedQueryable<Tag> FindUsedTags()
        {
            return FindAll().Where(x => x.Posts.Any()).OrderBy(x => x.Posts.Count);
        }
    }
}
